package com.example.star.service;

import com.example.star.entity.Favorite;
import com.example.star.entity.Follow;
import com.example.star.mapper.FollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FollowService {
    @Autowired
    private FollowMapper followMapper;

    public void saveFollow(Follow follow) {
        followMapper.insertFollow(follow);
    }

    public List<Follow> getAllFollows() {
        return followMapper.getAllFollows();
    }

    public List<Follow> getFollowByUserName(String userName){
        return followMapper.getFollowByUserName(userName);
    }

    public List<Follow> getFollowsByPage(int page, int size) {
        int offset = (page - 1) * size;
        return followMapper.getFollowsByPage(offset, size);
    }

    public int getTotalFollows() {
        return followMapper.getTotalFollows();
    }

    public void deleteFollowById(int id) {
        followMapper.deleteFollowById(id);
    }
    public Follow getFollowById(int id) {
        return followMapper.getFollowById(id);
    }

    public Follow getFollowByStarName(String starName) {
        return followMapper.getFollowByStarName(starName);
    }


    public boolean isFollow(String userName, String starName) {
        int count = followMapper.checkFollow(userName, starName);
        return count > 0;
    }

    public boolean deleteFollow(String userName, String starName) {
        int rowsAffected = followMapper.deleteFollow(userName, starName);
        return rowsAffected > 0;
    }

    // 根据用户名分页获取关注信息
    public List<Follow> getFollowsByUserName(String userName, int page, int size) {
        int offset = (page - 1) * size;
        return followMapper.getFollowsByUserName(userName, offset, size);
    }

    // 根据用户名获取关注信息的总数
    public int getTotalFollowsByUserName(String userName) {
        return followMapper.getTotalFollowsByUserName(userName);
    }

}